const db = require('../db/index.js');
const bcrypt = require('bcryptjs')
    // 用这个包来生成 Token 字符串
const jwt = require('jsonwebtoken')
    // 导入配置文件
const config = require('../config')

exports.reg = (req, res) => {
    const userinfo = req.body;
    if (!userinfo.username || !userinfo.password) {
        return res.send({ status: 1, message: '用户名或密码不能为空！' })
    }
    const query1 = `select * from sp_manager where mg_name=?`
    db.query(query1, [userinfo.username], (err, data) => {
        var obj = {
            data: {},
            meta: {
                status: '',
                msg: ''
            }
        }
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        }
        if (data.length > 0) {
            obj.meta.status = 500;
            obj.meta.msg = '用户名已被占用';
            return res.send(obj);
        }
        userinfo.password = bcrypt.hashSync(userinfo.password, 10)
        const query2 = `insert into sp_manager set ?`
        db.query(query2, { mg_name: userinfo.username, mg_pwd: userinfo.password }, (err, data) => {
            if (err) {
                obj.meta.status = 500;
                obj.meta.msg = err;
                return res.send(obj);
            }
            if (data.affectedRows !== 1) {
                obj.meta.status = 500;
                obj.meta.msg = '注册失败';
                return res.send(obj);
            }
            obj.meta.status = 200;
            obj.meta.msg = '注册成功！';
            return res.send(obj);
        })
    })
}
exports.login = (req, res) => {
    console.log(req.body)
    const sql = 'select * from sp_manager where mg_name=?'
    db.query(sql, req.body.username, function(err, results) {
        var obj = {
            data: {},
            meta: {
                status: '',
                msg: ''
            }
        }
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        } else {
            if (results.length === 1) {
                console.log(results)
                    // console.log(req.body.password)
                    // console.log(results[0].password)
                    //校验密码
                var compareResult = bcrypt.compareSync(req.body.password, results[0].mg_pwd)
                if (!compareResult) {
                    obj.meta.status = 400;
                    obj.meta.msg = '密码错误';
                    return res.send(obj);
                } else {
                    const user = {...results[0], mg_pwd: '' }
                        // 生成 Token 字符串
                    const tokenStr = jwt.sign(user, config.jwtSecretKey, {
                        expiresIn: '10h', // token 有效期为 10 个小时
                    })
                    obj.meta.status = 200;
                    obj.meta.msg = "登录成功";
                    obj.data.token = "Bearer " + tokenStr;
                    obj.data.id = results[0]['mg_id'];
                    obj.data.username = results[0]['mg_name'];
                    return res.send(obj);
                }
            } else {
                obj.meta.status = 400;
                obj.meta.msg = '用户名不存在';
                return res.send(obj);
            }


        }

    })

}